package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.TPurchase;
import com.ruoyi.system.domain.TSales;
import com.ruoyi.system.mapper.TSalesMapper;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.system.domain.TContract;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.print.attribute.standard.PrinterURI;

/**
 * 合同管理controller
 *
 * @date 2021-03-26
 */
@Controller
@RequestMapping("/system/contract")
public class TContractController extends BaseController
{
    private String prefix = "system/contract";

    @Autowired
    private ISysRoleService iSysRoleService;

    @Autowired
    private ITContractService tContractService;

    @Autowired
    private ITPurchaseService itPurchaseService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ITSalesService itSalesService;

    @GetMapping()
    public String contract()
    {
        return prefix + "/contract";
    }

    /**
     * 查询
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TContract tContract)
    {
        startPage();
        List<TContract> list = tContractService.selectTContractList(tContract);
        return getDataTable(list);
    }

    /** 查询合同*/
    @GetMapping("/contractShow/{contractId}")
    public String contractShow(@PathVariable("contractId") Long contractId,ModelMap modelMap){
        TContract tContract = tContractService.selectTContractById(contractId);
        modelMap.put("tContract",tContract);
        return prefix + "/pact";
    }

    /** 查询合同*/
    @GetMapping("/salesShow/{salesId}")
    public String salesShow(@PathVariable("salesId") Long salesId,ModelMap modelMap){
        TContract tContract = tContractService.selectTContractBySalesId(salesId);
        modelMap.put("tContract",tContract);
        return prefix + "/pact";
    }

    /**
     * 查询合同列表
     */
    @PostMapping("/listContract")
    @ResponseBody
    public String listContract(TContract tContract,ModelMap mmp)
    {
        TContract list = tContractService.selectContractById(tContract);
        mmp.put("list",tContract);
        return prefix + "contract";
    }


    /**
     * 导出
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TContract tContract)
    {
        List<TContract> list = tContractService.selectTContractList(tContract);
        ExcelUtil<TContract> util = new ExcelUtil<TContract>(TContract.class);
        return util.exportExcel(list, "contract");
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
//        List<SysRole> sysRoles = iSysRoleService.selectRoleListAll();
        List<SysUser> sysRoles = iSysUserService.selectUserListAll();
        mmap.put("sysUsers",sysRoles);
        List<TPurchase> tPurchases = itPurchaseService.selectTPurchaseListAll();
        mmap.put("tPurchases",tPurchases);
        List<TSales> tSales = itSalesService.selectTSalesListAll();
        mmap.put("tSales",tSales);
        return prefix + "/add";
    }

    /**
     * 新增保存
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TContract tContract)
    {
        return toAjax(tContractService.insertTContract(tContract));
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{contractId}")
    public String edit(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        TContract tContract = tContractService.selectTContractById(contractId);
        mmap.put("tContract", tContract);
//        List<SysRole> sysRoles = iSysRoleService.selectRoleListAll();
        List<SysUser> sysRoles = iSysUserService.selectUserListAll();
        mmap.put("sysUsers",sysRoles);
        List<TPurchase> tPurchases = itPurchaseService.selectTPurchaseListAll();
        mmap.put("tPurchases",tPurchases);
        List<TSales> tSales = itSalesService.selectTSalesListAll();
        mmap.put("tSales",tSales);
        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TContract tContract)
    {
        return toAjax(tContractService.updateTContract(tContract));
    }

    /**
     * 删除
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tContractService.deleteTContractByIds(ids));
    }
}